import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JTextPane;

/**
 * This class is Return GradesUI
 */
public class RetrunGradesUI extends JPanel{
	/**
	 * combo box for course 
	 */
	private JComboBox courseBox = null;
	/**
	 * combo box for student 
	 */
	private JComboBox StudentBox = null;
	/**
	 * add grade button
	 */
	private JButton btnGrdee = null;
	/**
	 * back button
	 */
	public JButton btnBack = null;
	/**
	 * title for the current window  
	 */
	private JLabel lbltitle = null;
	/**
	 * help button  
	 */
	public JButton btnHelp = null;
	/**
	 * grade label  
	 */
	private JLabel lblGrade = null;
	/**
	 * comment label  
	 */
	private JLabel lblEForm = null;
	private JTextField TextGrade = null;
	private JTextPane TextEForm = null;
	/**
	 * array of student
	 */
	private ArrayList<String> lstudent = new ArrayList<String>();
	/**
	 * array of course
	 */
	private ArrayList<String[]> lcourse = new ArrayList<String[]>();
	private int courseFlg = 0;
	private int studentFlg = 0;
	private int grd;
	private String eForm;
	/**
	 * save the current panel for the "back" function 
	 */
	private JPanel curPanel;
	/**
	 * logout button
	 */
	public JButton btnLogout = null;
	
	public RetrunGradesUI (){
		super();
		initialize();
	}

	private void initializeEmpty() {
		curPanel = this;
		lbltitle = new JLabel("Return Grades");
		lbltitle.setBounds(180, 28, 110, 14);
		this.add(lbltitle);
		
		JLabel lblNoCourses= new JLabel("You don't have any Courses ");
		lblNoCourses.setBounds(124,73, 190, 14);
		this.add(lblNoCourses);
		
		btnBack = new JButton("Back");
		btnBack.setBounds(187, 253, 86, 25);
		this.add(btnBack,null);
		this.setVisible(true);
		
		btnLogout = new JButton("Logout");     //Logout button
		btnLogout.setBounds(10, 11, 80, 19);
		add(btnLogout);
		btnLogout.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				  LogoutWin logt = new LogoutWin();
				  System.out.println("user press Logout button");
			}
		});
		
		btnHelp = new JButton("Help");
		btnHelp.setBounds(390, 11, 80, 19);
		btnHelp.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				  HelpWin help = new HelpWin ("Choose course type and click on 'Browse' button or ","on 'Define New Assignment' button");
				  System.out.println("user press Help button");
			}
		});
		this.add(btnHelp,null);
		
		this.setVisible(true);
		
		
		this.setSize(400, 321);
		this.setLayout(null);
		
	}

	public RetrunGradesUI(Object list) {
		super();
		lcourse = (ArrayList) list;
		initialize();
	}

	private void initialize() {
		curPanel = this;
		
		this.add(getCourseBox(), null);
		this.add(getStudentBox(), null);
		setStudentBox();
		
		lbltitle = new JLabel("Return Grades");
		lbltitle.setBounds(180, 28, 110, 14);
		this.add(lbltitle);
		
		lblEForm = new JLabel("Comment:");
		lblEForm.setBounds(180, 100, 110, 14);
		this.add(lblEForm);	
			
		TextEForm = new JTextPane();
		TextEForm.setBounds(new Rectangle(173, 125, 169, 92));
		TextEForm.addFocusListener(new java.awt.event.FocusAdapter() 
		{
			public void focusLost(java.awt.event.FocusEvent e) 
			{
				System.out.println("focusLost()"); 
				eForm=TextEForm.getText();			
			}
		}
		);
		this.add(TextEForm,null);
		
		lblGrade = new JLabel("Grade:");
		lblGrade.setBounds(95, 100, 110, 14);
		this.add(lblGrade);	
			
		TextGrade = new JTextField();
		TextGrade.setBounds(new Rectangle(95, 118, 68, 25));
		TextGrade.addFocusListener(new java.awt.event.FocusAdapter() 
		{
			public void focusLost(java.awt.event.FocusEvent e) 
			{
				System.out.println("focusLost()"); 
				grd = Integer.parseInt(TextGrade.getText());
			}
		}
		);
		this.add(TextGrade,null);

		
		
		btnGrdee = new JButton();
		btnGrdee.setBounds(new Rectangle(137, 238, 195, 25));
		btnGrdee.setText("Add Grade");
		btnGrdee.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
					TextGrade.setText(null);
					TextEForm.setText(null);
					System.out.println("Add Grade");
					Grades grade = new Grades(lstudent.get(studentFlg-1),(lcourse.get(courseFlg))[1],grd,eForm,(lcourse.get(courseFlg))[0],Global.logIn.getUserlogin());
					SQL s = new SQL(grade,50);
					s.accept();
					if (s.getMsg().equals(true))
					{
						MsgWin msgWin = new MsgWin("Your Grade sent"); 	
					}
			}
		});
		this.add(btnGrdee);
	
		btnBack = new JButton("Back");
		btnBack.setBounds(191, 267, 86, 25);
		this.add(btnBack,null);
		this.setVisible(true);
		
		btnLogout = new JButton("Logout");     //Logout button
		btnLogout.setBounds(10, 11, 80, 19);
		add(btnLogout);
		btnLogout.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				  LogoutWin logt = new LogoutWin();
				  System.out.println("user press Logout button");
			}
		});
		
		btnHelp = new JButton("Help");
		btnHelp.setBounds(390, 11, 80, 19);
		btnHelp.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				  HelpWin help = new HelpWin ("Choose course type and click on 'Browse' button or ","on 'Define New Assignment' button");
				  System.out.println("user press Help button");
			}
		});
		this.add(btnHelp,null);
		
		this.setVisible(true);
		
		
		this.setSize(400, 321);
		this.setLayout(null);
		
		JLabel lblCourse = new JLabel("Course:");
		lblCourse.setBounds(50, 69, 110, 14);
		add(lblCourse);
		
		JLabel lblStudent = new JLabel("Student:");
		lblStudent.setBounds(200, 69, 110, 14);
		add(lblStudent);
		
	}
	
	private void setStudentBox() {
		studentFlg = 0;
		if (StudentBox!= null)
			StudentBox.removeAllItems();
		String arr[] = new String[3];
		arr[0] = Global.logIn.getUserlogin();
		arr[1] = (lcourse.get(courseFlg)[1]);
		arr[2] = (lcourse.get(courseFlg)[0]);
		SQL s2 = new SQL(arr,53);
		s2.accept();
		lstudent=(ArrayList<String>)s2.getMsg();
		int size = lstudent.size(); 
		System.out.println("size is :" +size);
			for (int i=0; i<size ; i++)
			{	
				SQL s3 = new SQL ((lstudent.get(i)),40);
				s3.accept();
				StudentBox.addItem((String)s3.getMsg());
			}
	}

	private JComboBox getCourseBox() {
		if (courseBox == null) {
			courseBox = new JComboBox();
			courseBox.setBounds(new Rectangle(95, 64, 95, 25));  
			int size = lcourse.size();
			String cID;
				for (int i=0; i<size ; i++)
				{
					cID = lcourse.get(i)[1];
					SQL s = new SQL (cID,18);
					s.accept();
					Object msg = s.getMsg();
					System.out.println("this course : " + i + cID); 
					courseBox.addItem((String)msg);
				}
				
			courseBox.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					System.out.println("itemStateChanged()"); 
					courseFlg = courseBox.getSelectedIndex();
					System.out.println("this is : " + courseFlg);
					setStudentBox();
				}
			});
		}
		return courseBox;
	}
	
	
	private JComboBox getStudentBox() {
		
		StudentBox = new JComboBox();
		StudentBox.setBounds(new Rectangle(252, 64, 95, 25));  
			
		StudentBox.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					System.out.println("itemStateChanged()"); 
					studentFlg = StudentBox.getSelectedIndex();
					studentFlg++;
					System.out.println("this is : " + studentFlg); 
				}
			});
		
		return StudentBox;
	}
}
	